{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "    该算法用python实现插入排序算法\n",
    "    author: fuhongyu\n",
    "    time: 2018-07-27\n",
    "    时间复杂度：O(n^2)\n",
    "    目标： 十分钟内写出次代码\n",
    "'''\n",
    "def inser_sort(lists):\n",
    "    for i in range(1,len(lists)):\n",
    "        key = lists[i]\n",
    "        j = i-1\n",
    "        # 下面将key插入到已排好序的list中\n",
    "        while j >= 0:\n",
    "            if lists[j] > key:\n",
    "                lists[j+1] = lists[j]\n",
    "                lists[j] = key\n",
    "            j -= 1\n",
    "    return lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 2, 3, 3, 4, 6, 7, 9, 10, 30]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以下测试代码\n",
    "lists = [2,4,6,3,9,7,3,0,2,10,30]\n",
    "inser_sort(lists)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
